#include "Bullet.h"

Bullet::Bullet()
	: mActive(false)
{
}

Bullet::~Bullet()
{
}

void Bullet::Load()
{
	mSprite.Load("bullet1.png");
}

void Bullet::Unload()
{
	mSprite.Unload();
}

void Bullet::Update(float deltaTime)
{
	if (mActive)
	{
		mPosition += mVelocity * deltaTime;
		
		const int kWinWidth = IniFile_GetInt("WinWidth", 800);
		const int kWinHeight = IniFile_GetInt("WinHeight", 600);
		const int kThreshold = mSprite.GetWidth();
		if (mPosition.x < -kThreshold || mPosition.x > kWinWidth + kThreshold ||
			mPosition.y < -kThreshold || mPosition.y > kWinHeight + kThreshold)
		{
			Kill();
		}
	}
}

void Bullet::Render()
{
	if (mActive)
	{
		const int width = mSprite.GetWidth();
		const int height = mSprite.GetHeight();
		SVector2 offset(width * 0.5f, height * 0.5f);
		SVector2 renderPosition = mPosition - offset;
		mSprite.SetPosition(renderPosition);
		mSprite.Render();
	}
}

void Bullet::Fire(const SVector2& pos, const SVector2& vel)
{
	mPosition = pos;
	mVelocity = vel;
	mActive = true;
}

void Bullet::Kill()
{
	mActive = false;
}